package com.xci.xajtfb.wech;

import cn.binarywang.wx.miniapp.constant.WxMaConstants;

import cn.binarywang.wx.miniapp.api.WxMaService;
import cn.binarywang.wx.miniapp.bean.WxMaJscode2SessionResult;
import cn.binarywang.wx.miniapp.bean.WxMaPhoneNumberInfo;
import cn.binarywang.wx.miniapp.bean.WxMaSubscribeMessage;
import cn.binarywang.wx.miniapp.bean.WxMaUserInfo;
import com.github.lvyanyang.annotation.RequestJson;
import com.github.lvyanyang.core.BoolResult;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.common.error.WxErrorException;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * 微信小程序用户接口
 */
@Slf4j
@RestController
@RequestMapping("/wxmini")
public class WxMaUserController {
    @Resource private WxMaService wxService;
    /**
     * 登录凭证校验
     */
    @PostMapping("/code2Session")
    public BoolResult<WxMaJscode2SessionResult> code2Session(@RequestJson String appid, @RequestJson String code) throws WxErrorException {
        if (StringUtils.isBlank(code)) {
            return BoolResult.fail("请输入用户登录凭证code");
        }
        WxMaJscode2SessionResult session = wxService.getUserService().getSessionInfo(code);
        return BoolResult.success(session);
    }

    /**
     * 获取用户信息接口
     */
    @PostMapping("/userInfo")
    public BoolResult<WxMaUserInfo> userInfo(@RequestJson String appid,
                                             @RequestJson String sessionKey,
                                             @RequestJson String encryptedData,
                                             @RequestJson String iv) {
        WxMaUserInfo userInfo = wxService.getUserService().getUserInfo(sessionKey, encryptedData, iv);
        return BoolResult.success(userInfo);
    }

    /**
     * 获取用户绑定手机号信息
     */
    @PostMapping("/phoneNumber")
    public BoolResult<WxMaPhoneNumberInfo> phoneNumber(@RequestJson String appid, @RequestJson String sessionKey,
                                                       @RequestJson String encryptedData, @RequestJson String iv) {
        WxMaPhoneNumberInfo phoneNoInfo = wxService.getUserService().getPhoneNoInfo(sessionKey, encryptedData, iv);
        return BoolResult.success(phoneNoInfo);
    }

    /**
     * 发送订阅消息
     */
    @PostMapping("/sendSubscribe")
    public BoolResult sendSubscribe(@RequestJson String appid, @RequestJson String openid,
                           @RequestJson String page, @RequestJson String templateId) throws WxErrorException {
        WxMaSubscribeMessage message = new WxMaSubscribeMessage();
        message.setToUser(openid);
        message.setTemplateId(templateId);
        message.setPage(page);
        message.setMiniprogramState(WxMaConstants.MiniProgramState.DEVELOPER);
        message.addData(new WxMaSubscribeMessage.MsgData("thing1", "陕U985"));
        message.addData(new WxMaSubscribeMessage.MsgData("time2", "2021年03月05日"));
        message.addData(new WxMaSubscribeMessage.MsgData("thing4", "智慧岛五号线"));
        message.addData(new WxMaSubscribeMessage.MsgData("thing3", "我们邀请你评价此次乘车体验"));

        log.debug("开始发送订阅消息");
        wxService.getSubscribeService().sendSubscribeMsg(message);
        return BoolResult.success();
    }
}
